home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 12984 / 12984.xpi / chrome / VideoDownloaderToolbar.jar / content / facebook.js < prev    next >
Text File  |  2010-01-29  |  10KB  |  376 lines

  1. if(!com) var com={};
  2. if(!com.VidBar) com.VidBar={};
  3.  
  4. com.VidBar.VidFB = {
  5.     FB_ROOT: 'http://www.facebook.com/',
  6.     FB_API: 'http://api.facebook.com/restserver.php',
  7.  
  8.     pref : null,
  9.     xmlHttp : null,
  10.     api_key : 'f883215e0f13977f4c4f8019876fdc96',
  11.     fsecret_key : 'f7bcacb9f8692e7ac235063cd9f9be30',
  12.     secret_key : '',
  13.     session_key : '',
  14.     uid : '',
  15.     auth_token : '',
  16.  
  17.     getting_session : false,
  18.  
  19.     url : {
  20.         root : this.FB_ROOT,
  21.         api : this.FB_API,
  22.         login : this.FB_ROOT + 'login.php',
  23.         logout : this.FB_ROOT + 'logout.php', // POST confirm=1
  24.         search : this.FB_ROOT + 's.php', // q=searchString
  25.         profile : this.FB_ROOT + 'profile.php', // id
  26.         poke : this.FB_ROOT + 'poke.php', // id
  27.         message : this.FB_ROOT + 'message.php', // id, subject, msg
  28.         addfriend : this.FB_ROOT + 'addfriend.php', // id
  29.         photos : this.FB_ROOT + 'photos.php', // id
  30.         photo_search : this.FB_ROOT + 'photo_search.php', // id
  31.         wall : this.FB_ROOT + 'wall.php', // id
  32.         notes : this.FB_ROOT + 'notes.php' // id
  33.     },
  34.  
  35.     getPref : function(p) {
  36.         return this.pref.getFBPref(p);
  37.     },
  38.  
  39.     setPref : function(p, v) {
  40.         this.pref.setFBPref(p, v);
  41.     },
  42.  
  43.     init : function(pref) {
  44.         this.pref = pref;
  45.  
  46.         if (this.getPref("session_key") != '')
  47.             this.session_key = this.getPref("session_key");
  48.         if (this.getPref("secret_key") != '')
  49.             this.secret_key = this.getPref("secret_key");
  50.  
  51.         if (!this.isLogedIn()) {
  52.             return;
  53.         } else {
  54.             this.validateAuth();
  55.         }
  56.     },
  57.  
  58.     isLogedIn : function() {
  59.         return !(this.session_key == '' || this.secret_key == '');
  60.     },
  61.  
  62.     validateAuth : function() {
  63.         var query = [];
  64.         query.push('api_key=' + this.api_key);
  65.         query.push('format=JSON');
  66.         query.push('v=1.0');
  67.         query.push('session_key=' + this.session_key);
  68.         query.push('call_id=' + this.getCallId());
  69.         query.push('method=facebook.users.getLoggedInUser');
  70.         query.sort();
  71.         query.push('sig=' + md5(query.join('') + this.secret_key));
  72.  
  73.         var self = this;
  74.         this.postQuery(this.url.api, query.join('&'), function() {
  75.                     if (self.xmlHttp == null)
  76.                         return;
  77.                     if (self.xmlHttp.readyState == 4) {
  78.                         if (self.xmlHttp.status != 200) {
  79.                             self.xmlHttp = null;
  80.                             return;
  81.                         }
  82.                         var s = self.xmlHttp.responseText;
  83.                         if (s == null) {
  84.                             return "Couldn't connect";
  85.                         } else {
  86.                             var d = JSON.parse(s);
  87.                             // com.VidBar.__d(s);
  88.                             self.getNotifications();
  89.                             if (d.error_code) {
  90.                                 self.setPref("session_key", "");
  91.                                 self.setPref("secret_key", "");
  92.                                 self.session_key = "";
  93.                                 self.secret_key = "";
  94.                             } else {
  95.                                 self.getNotifications();
  96.                             }
  97.                         }
  98.                     }
  99.                 });
  100.     },
  101.  
  102.     /*
  103.      * # AUTHORIZATION
  104.      */
  105.     startAuthorization : function() {
  106.         var query = [];
  107.         query.push('api_key=' + this.api_key);
  108.         query.push('v=1.0');
  109.         query.push('format=JSON');
  110.         query.push('method=facebook.auth.createToken');
  111.         query.sort();
  112.         query.push('sig=' + md5(query.join('') + this.fsecret_key));
  113.  
  114.         var self = this;
  115.         this.postQuery(this.url.api, query.join('&'), function() {
  116.                     if (self.xmlHttp == null)
  117.                         return;
  118.                     if (self.xmlHttp.readyState == 4) {
  119.                         if (self.xmlHttp.status != 200) {
  120.                             self.xmlHttp = null;
  121.                             return;
  122.                         }
  123.                         var s = self.xmlHttp.responseText;
  124.                         if (s == null) {
  125.                             return "Couldn't connect";
  126.                         } else {
  127.                             var d = JSON.parse(s);
  128.                             self.auth_token = d;
  129.                             self.openLogin(self.url.login + "?api_key="
  130.                                     + self.api_key + "&v=1.0&auth_token=" + d);
  131.                         }
  132.                     }
  133.                 });
  134.     },
  135.  
  136.     getSession : function() {
  137.         var query = [];
  138.         query.push('api_key=' + this.api_key);
  139.         query.push('auth_token=' + this.auth_token);
  140.         query.push('format=JSON');
  141.         query.push('v=1.0');
  142.         query.push('method=facebook.auth.getSession');
  143.         query.sort();
  144.         query.push('sig=' + md5(query.join('') + this.fsecret_key));
  145.  
  146.         var self = this;
  147.         this.postQuery(this.url.api, query.join('&'), function() {
  148.                     if (self.xmlHttp == null)
  149.                         return;
  150.                     if (self.xmlHttp.readyState == 4) {
  151.                         if (self.xmlHttp.status != 200) {
  152.                             self.xmlHttp = null;
  153.                             return;
  154.                         }
  155.                         var s = self.xmlHttp.responseText;
  156.                         if (s == null) {
  157.                             return "Couldn't connect";
  158.                         } else {
  159.                             var d = JSON.parse(s);
  160.                             self.session_key = d.session_key;
  161.                             self.uid = d.uid;
  162.                             self.secret_key = d.secret;
  163.                             if (d.expires == 0)
  164.                                 self.setPref("session_key", d.session_key);
  165.                             self.setPref("secret_key", d.secret);
  166.                             self.getNotifications();
  167.                         }
  168.                     }
  169.                     //self.getting_session = false;
  170.                 });
  171.     },
  172.     /*
  173.      * # NOTIFICATIONS
  174.      */
  175.     getNotifications : function(manual) {
  176.         var query = [];
  177.         query.push('api_key=' + this.api_key);
  178.         query.push('session_key=' + this.session_key);
  179.         query.push('call_id=' + this.getCallId());
  180.         query.push('format=JSON');
  181.         query.push('v=1.0');
  182.         query.push('method=facebook.notifications.get');
  183.         query.sort();
  184.         query.push('sig=' + md5(query.join('') + this.secret_key));
  185.  
  186.         var self = this;
  187.         this.postQuery(this.url.api, query.join('&'), function() {
  188.                     self.proccessNotifications()
  189.                 });
  190.         if (!manual)
  191.             setTimeout(function() {
  192.                         self.getNotifications()
  193.                     }, 60000);
  194.     },
  195.     unreads : {
  196.         message : '',
  197.         pokes : '',
  198.         friend_requests : {
  199.             len : 0,
  200.             first : 0
  201.         },
  202.         group_invites : {
  203.             len : 0,
  204.             first : 0
  205.         },
  206.         event_invites : {
  207.             len : 0,
  208.             first : 0
  209.         }
  210.     },
  211.     proccessNotifications : function() {
  212.         if (this.xmlHttp == null)
  213.             return;
  214.             
  215.         if (this.xmlHttp.readyState == 4) {
  216.             if (this.xmlHttp.status != 200) {
  217.                 this.xmlHttp = null;
  218.                 return;
  219.             }
  220.             var s = this.xmlHttp.responseText;
  221.             if (s == null) {
  222.                 return "Couldn't connect";
  223.             } else {
  224.                 var d = JSON.parse(s);
  225.  
  226.                 if (d.messages.unread > 0
  227.                         && d.messages.most_recent != this.unreads.message) {
  228.                     this.unreads.message = d.messages.most_recent;
  229.                 }
  230.                 document.getElementById("vidbar-fb-mail").label = "("
  231.                         + d.messages.unread + ")";
  232.  
  233.                 if (d.pokes.unread > 0
  234.                         && d.pokes.most_recent != this.unreads.pokes) {
  235.                     this.unreads.pokes = d.pokes.most_recent;
  236.                 }
  237.                 document.getElementById("vidbar-fb-poke").label = "("
  238.                         + d.pokes.unread + ")";
  239.  
  240.                 if (d.friend_requests.length) {
  241.                     document.getElementById("vidbar-fb-friend").label = "("
  242.                             + d.friend_requests.length + ")";
  243.                     if (d.friend_requests.length != this.unreads.friend_requests.len
  244.                             || d.friend_requests[0] != this.unreads.friend_requests.first) {
  245.                         this.unreads.friend_requests.first = d.friend_requests[0];
  246.                         this.unreads.friend_requests.len = d.friend_requests.length;
  247.                     }
  248.                 } else {
  249.                     document.getElementById("vidbar-fb-friend").label = "(0)";
  250.                 }
  251.  
  252.                 if (d.group_invites.length) {
  253.                     document.getElementById("vidbar-fb-group").label = "("
  254.                             + d.group_invites.length + ")";
  255.                     if (d.group_invites.length != this.unreads.group_invites.len
  256.                             || d.group_invites[0] != this.unreads.group_invites.first) {
  257.                         this.unreads.group_invites.first = d.group_invites[0];
  258.                         this.unreads.group_invites.len = d.group_invites.length;
  259.                     }
  260.                 } else {
  261.                     document.getElementById("vidbar-fb-group").label = "(0)";
  262.                 }
  263.  
  264.                 if (d.event_invites.length) {
  265.                     document.getElementById("vidbar-fb-event").label = "("
  266.                             + d.event_invites.length + ")";
  267.                     if (d.event_invites.length != this.unreads.event_invites.len
  268.                             || d.event_invites[0] != this.unreads.event_invites.first) {
  269.                         this.unreads.event_invites.first = d.event_invites[0];
  270.                         this.unreads.event_invites.len = d.event_invites.length;
  271.                     }
  272.                 } else {
  273.                     document.getElementById("vidbar-fb-event").label = "(0)";
  274.                 }
  275.             }
  276.         }
  277.     },
  278.  
  279.     openURLInNewTab : function(url) {
  280.         com.VidBar.__d("FB: openURLInNewTab");
  281.         var browser = window.getBrowser();
  282.         var tab = browser.addTab(url);
  283.  
  284.         setTimeout(function(b, t) {
  285.                     b.selectedTab = t;
  286.                 }, 0, browser, tab);
  287.     },
  288.     openMail : function() {
  289.         if (!this.isLogedIn()) {
  290.             this.startAuthorization();
  291.             return;
  292.         }
  293.         this.openURLInNewTab("http://www.facebook.com/inbox/");
  294.     },
  295.     openFriend : function() {
  296.         if (!this.isLogedIn()) {
  297.             this.startAuthorization();
  298.             return;
  299.         }
  300.         this
  301.                 .openURLInNewTab("http://www.facebook.com/reqs.php");
  302.     },
  303.     openEvent : function() {
  304.         if (!this.isLogedIn()) {
  305.             this.startAuthorization();
  306.             return;
  307.         }
  308.         this.openURLInNewTab("http://www.facebook.com/reqs.php");
  309.     },
  310.     openGroup : function() {
  311.         if (!this.isLogedIn()) {
  312.             this.startAuthorization();
  313.             return;
  314.         }
  315.         this.openURLInNewTab("http://www.facebook.com/reqs.php");
  316.     },
  317.     openPoke : function() {
  318.         if (!this.isLogedIn()) {
  319.             this.startAuthorization();
  320.             return;
  321.         }
  322.         this.openURLInNewTab("http://www.facebook.com/home.php");
  323.     },
  324.  
  325.     /*
  326.      * UTILITY FUNCTIONS
  327.      */
  328.     postQuery : function(url, query, onSuccess) {
  329.         this.xmlHttp = new XMLHttpRequest();
  330.         this.xmlHttp.onreadystatechange = onSuccess;
  331.         this.xmlHttp.open("POST", url);
  332.         this.xmlHttp.setRequestHeader("Content-Type",
  333.                 "application/x-www-form-urlencoded");
  334.         this.xmlHttp.setRequestHeader("Cache-Control", "no-cache");
  335.         this.xmlHttp.setRequestHeader("Accept", "text/xml");
  336.         this.xmlHttp.send(query);
  337.     },
  338.  
  339.     openLogin : function(url) {
  340.         com.VidBar.__d("FB: openLogin");
  341.  
  342.         var tab = gBrowser.addTab(url);
  343.         var browser = gBrowser.getBrowserForTab(tab);
  344.         gBrowser.selectedTab = tab;
  345.  
  346.         var self = this;
  347.         this.onLoginLoad = function() {
  348.                     self.checkLogin(browser.contentDocument, browser);
  349.                 };
  350.         browser.addEventListener("load", this.onLoginLoad, true);
  351.     },
  352.  
  353.     checkLogin : function(doc, browser) {
  354.         com.VidBar.__d("FB: checkLogin: " + doc.location.toString());
  355.  
  356.         if (this.isLogedIn())
  357.             return;
  358.             
  359. //        if (this.getting_session==true)
  360. //            return;
  361.             
  362.         if (doc.location.toString()
  363.                 .indexOf("https://ssl.facebook.com/desktopapp.php") != -1) {
  364.             if(this.onLoginLoad)
  365.                 browser.removeEventListener("load", this.onLoginLoad, true);
  366.                 
  367.             //this.getting_session=true;
  368.             this.getSession();
  369.         }
  370.     },
  371.  
  372.     getCallId : function() {
  373.         return (new Date()).getTime();
  374.     }
  375. };
  376.